//
//  DBThermemoter.m
//  CareRingApp
//
//  Created by Linktop on 2022/6/7.
//

#import "DBThermemoter.h"
#import "TimeUtils.h"

#import "DBHistoryDataSr03.h"

#import "NSNumber+Imperial.h"

//NSString * const TABLE_NM_THERMEMOTER = @"table_ther";
//const float CALC_MIN = 35.2; //最低计算阈值

@implementation DBThermemoter
//创建数据库


//-(void)insert:(void(^_Nullable)(BOOL succ))complete
//{
//    [super insertBy:TABLE_NM_THERMEMOTER CMP:complete];
//
////    DB_WEAK_SELF
////    [[HMDBManager instance].dbQueue inDatabase:^(FMDatabase * _Nonnull db) {
////        DB_STRONG_SELF
//////        [db beginTransaction];
////
////        NSString *sqlString = [NSString stringWithFormat:  @"INSERT INTO %@ (userId, value, time) VALUES (?,?,?)", TABLE_NM_THERMEMOTER];
////        BOOL succ = [db executeUpdate:sqlString,
////                     strongSelf.userId,
////                     strongSelf.value,
////                     strongSelf.time
////                    ];
////
//////        [db commit];
////
////        dispatch_async(dispatch_get_main_queue(), ^{
////            if (complete) {
////                complete(succ);
////            }
////        });
////
////    }];
//
//}

// 时间段内最小值
//+(NSNumber *)minValueFrom:(NSDate *)beginDate End:(NSDate *)endDate Account:(NSString *)account Db:(FMDatabase * _Nonnull )db
//{
//    NSString *sql =  [NSString stringWithFormat:@"SELECT MIN(value) as minValue FROM %@ WHERE account = ? AND time BETWEEN ? AND ?", TABLE_NM_THERMEMOTER];
//    FMResultSet *rs = [db executeQuery:sql, account, beginDate, endDate];
//    NSNumber * min = nil;
//    while ([rs next]) {
//        min = @([rs doubleForColumn:@"minValue"]);
//
//    }
//    [rs close];
//
//    return min;
//}

// 时间段内平局值
//+(NSNumber *)avgValueFrom:(NSDate *)beginDate End:(NSDate *)endDate Account:(NSString *)account Db:(FMDatabase * _Nonnull )db
//{
//    NSString *sql =  [NSString stringWithFormat:@"SELECT AVG(value) as avgValue FROM %@ WHERE account = ? AND time BETWEEN ? AND ? AND value >= ?", TABLE_NM_THERMEMOTER];
//    FMResultSet *rs = [db executeQuery:sql, account, beginDate, endDate, @(0)];
//    NSNumber * min = nil;
//    while ([rs next]) {
//        min = @([rs doubleForColumn:@"avgValue"]);
//
//    }
//    [rs close];
//
//    return min;
//}


+(void)queryMutiDataGroupHours:(NSString * _Nonnull)account DayDate:(NSDate *)dayDate   Cpmplete:(void(^)(NSMutableArray<NSDictionary *> * res) )cmpBlk
{

}




+(void)queryBy:(NSString * _Nonnull)macAddress Begin:(NSDate *)beginDate EndDate:(NSDate *)endDate OrderBeTimeDesc:(BOOL)isDesc Cpmplete:(void(^)(NSMutableArray<DBThermemoter *> *results, NSNumber *maxThermemoter, NSNumber *minThermemoter, NSNumber *avgThermemoter))cmpBlk
{
    if (!macAddress.length) {
        return;
    }
    
    [DBHistoryDataSr03 queryBy:macAddress Begin:beginDate End:endDate OrderBeTimeDesc:isDesc Cpmplete:^(NSMutableArray<DBHistoryDataSr03 *> * _Nonnull results) {
        
        NSMutableArray<DBThermemoter *> *therRes = [NSMutableArray new];
        NSNumber *maxThermemoter, *minThermemoter, *avgThermemoter;
        NSInteger count = 0;
       
        for (DBHistoryDataSr03 *hisObj in results) {
            
     
            DBThermemoter *therModel = [[DBThermemoter alloc]init];
            
            therModel.value = hisObj.skinTemp;
            therModel.time = [NSDate dateWithTimeIntervalSince1970:hisObj.time.doubleValue];
            therModel.macAddress = hisObj.macAddress;
            therModel.battery = hisObj.battery;
            [therRes addObject:therModel];
            if (maxThermemoter == nil) { // 最大
                maxThermemoter = hisObj.skinTemp;
            } else if (maxThermemoter.floatValue < hisObj.skinTemp.floatValue) {
                maxThermemoter = hisObj.skinTemp;
            }
            
            if (minThermemoter == nil) { // 最小
                minThermemoter = hisObj.skinTemp;
            } else if (minThermemoter.floatValue > hisObj.skinTemp.floatValue) {
                minThermemoter = hisObj.skinTemp;
            }
            
            if (hisObj.skinTemp) { // 平均
                avgThermemoter = @(avgThermemoter.floatValue + hisObj.skinTemp.floatValue);
                count++;
            }
            
        }
        if (count > 0) {
            avgThermemoter = @(avgThermemoter.floatValue / count);
        }
        if (cmpBlk) {
            dispatch_async(dispatch_get_main_queue(), ^{
                cmpBlk(therRes, maxThermemoter, minThermemoter, avgThermemoter);
            });
        }
        
    }];
}

/// 查询账号关联的数据的最大时间戳
/// @param account 账号
/// @param cmpBlk 回调
//+(void)queryMaxTime:(NSString * _Nonnull)account Cpmplete:(void(^)(NSNumber * _Nullable maxTimeStamp) )cmpBlk
//{
//    [super queryMaxTime:account TableName:TABLE_NM_THERMEMOTER Cpmplete:cmpBlk];
//}

//+(void)deleteAll {
//    [super deleteAll:TABLE_NM_THERMEMOTER];
//}



#pragma mark -- HMPdfShareProtoal
//- (NSArray<NSString *> *)pdfShowValuerStrings:(NSInteger)index
//{
//    NSArray *resArray = nil;
//    NSString *noStr = [NSString stringWithFormat:@"%ld", index];
//
//    NSString *dateStr = [self.time formatPdfShareTime:_L(L_PDF_TIME_FORMAT)];
//
//
//    NSString *fluStr = @"";
//
//    fluStr = [NSString stringWithFormat:@"%.1f%@", self.value, _L(L_UNIT_TEMP_C)];
//
//
//    resArray = @[noStr, dateStr,  fluStr];
//    return resArray;
//}

@end
